package djboom.materials.display.elements {
	import djboom.materials.events.TableDataCellEvent;
	import djboom.materials.state.Font;

	import org.casalib.display.CasaShape;
	import org.casalib.display.CasaSprite;
	import org.casalib.display.CasaTextField;

	import flash.display.CapsStyle;
	import flash.display.JointStyle;
	import flash.display.LineScaleMode;
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.KeyboardEvent;
	import flash.text.AntiAliasType;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import flash.ui.Keyboard;

	/**
	 * .
	 * <br>
	 * 
	 * @author DJ BooM 
	 * @since 19.05.2010
	 * 
	 */

	public class TableDataCell extends CasaSprite {

		// ----------------------------------------------------------------------------
		// Константы класса.
		// ----------------------------------------------------------------------------

		public static const CELL_FORMAT_19PX:TextFormat = new TextFormat(Font.MYRIAD_PRO_COND, 19, 0x004064, null, true);

		
		// ----------------------------------------------------------------------------
		// Свойства класса.
		// ----------------------------------------------------------------------------

		private var _cellField:CasaTextField;
		private var _cellWidth:int;
		private var _shapeCell:CasaShape;
		private var _checkData:String;
		private var _strokeCell:CasaShape;
		private var _lastCell:Boolean = false;
		private var _lastCellRow:Boolean = false;

		// ----------------------------------------------------------------------------
		// Конструктор.
		// ----------------------------------------------------------------------------

		public function TableDataCell(cellAlign:String, cellWidth:int, cellData:String, checkData:String, cellTextFormat:TextFormat) {
			super();
			
			_cellWidth = cellWidth;
			_checkData = checkData;
			
			_shapeCell = new CasaShape();
			_shapeCell.graphics.beginFill(0xffd4dc);
			_shapeCell.graphics.drawRect(0, 0, _cellWidth, 35);
			_shapeCell.graphics.endFill();
			_shapeCell.alpha = 0; 			addChild(_shapeCell);
			
			_cellField = new CasaTextField();
			_cellField.embedFonts = true;
			_cellField.selectable = false;
			_cellField.defaultTextFormat = cellTextFormat;
			_cellField.width = _cellWidth;
			_cellField.autoSize = cellAlign;
			_cellField.antiAliasType = AntiAliasType.ADVANCED;
			_cellField.mouseEnabled = false;
			_cellField.thickness = 50;
			_cellField.y = 5;
			_cellField.tabEnabled = false;
			
			if (cellAlign == TextFieldAutoSize.LEFT) _cellField.x = 5;
			
			if (cellData != null) _cellField.text = cellData;
			
			addChild(_cellField);

			addEventListener(Event.REMOVED_FROM_STAGE, clearContent);
		}	 

		// ----------------------------------------------------------------------------
		// Методы класса.
		// ----------------------------------------------------------------------------

		private function clearContent(event:Event):void {
			_cellField.destroy();
			_cellField = null;
			
			_shapeCell.destroy();
			_shapeCell = null;
			
			if (_strokeCell != null) {
				_strokeCell.destroy();
				_strokeCell = null;
			}
		}

		public function get cellWidth():int {
			return _cellWidth;
		}

		// ----------------------------------------------------------------------------
		// Публичные методы.
		// ----------------------------------------------------------------------------

		public function enableInput():void {
			_cellField.text = "";
			
			_strokeCell = new CasaShape();
			_strokeCell.graphics.lineStyle(4, 0xca1235, 1, false, LineScaleMode.VERTICAL, CapsStyle.SQUARE, JointStyle.MITER);
			_strokeCell.graphics.drawRect(0, 0, _cellWidth, 35);
			addChild(_strokeCell);
			
			_shapeCell.alpha = 1;
			
			_cellField.selectable = true;
			_cellField.mouseEnabled = true;
			_cellField.type = TextFieldType.INPUT;
			
			_cellField.maxChars = 9;			_cellField.restrict = "0-9,.";
			
			stage.focus = _cellField;
			
			_cellField.addEventListener(KeyboardEvent.KEY_DOWN, checkDataKey);
			_cellField.addEventListener(FocusEvent.FOCUS_OUT, checkDataFocus);		}

		private function checkDataFocus(event:FocusEvent):void {
			checkInputData();
		}

		private function checkDataKey(event:KeyboardEvent):void {
			if (event.keyCode == Keyboard.ENTER || event.keyCode == Keyboard.RIGHT) {
				_cellField.removeEventListener(FocusEvent.FOCUS_OUT, checkDataFocus);
				
				checkInputData();
			}
		}

		private function checkInputData():void {
			
			
			if (_cellField.text.indexOf(",") != -1) {
				var i:Array = _cellField.text.split(",");
				_cellField.text = new String().concat(i[0], ".", i[1]);
			}
			
			if (_cellField.text == _checkData) {
                    
				if (_lastCellRow) {
					if (_lastCell) {
						dispatchEvent(new TableDataCellEvent(TableDataCellEvent.INPUT_LAST_CELL));
					} else {
						dispatchEvent(new TableDataCellEvent(TableDataCellEvent.INPUT_LAST_CELL_ROW));
					}
				} else {
					dispatchEvent(new TableDataCellEvent(TableDataCellEvent.INPUT_SUCCESS));
				}
			} else {
				_cellField.text = "";
                    
				dispatchEvent(new TableDataCellEvent(TableDataCellEvent.INPUT_ERROR));
			}
                
			disableInput();
		}

		private function disableInput():void {
			if (_cellField != null) {
				_cellField.selectable = false;
				_cellField.mouseEnabled = false;
				_cellField.type = TextFieldType.DYNAMIC;
			
				_cellField.removeEventListener(KeyboardEvent.KEY_DOWN, checkDataKey);
				_cellField.removeEventListener(FocusEvent.FOCUS_OUT, checkDataFocus);
			}
			
			if (_shapeCell != null) {
				_shapeCell.alpha = 0;
			}
			
			if (_strokeCell != null) {
				_strokeCell.destroy();
				_strokeCell = null;
			}
		}

		public function set lastCell(lastCell:Boolean):void {
			_lastCell = lastCell;
		}

		public function set lastCellRow(lastCellRow:Boolean):void {
			_lastCellRow = lastCellRow;
		}
			
			// ----------------------------------------------------------------------------
	        // Публичные свойства.
	        // ----------------------------------------------------------------------------
	}
}
